package cn.nnb.control.cy;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.CachedRowSet;

import com.snow.order.biz.FoodBiz;
import com.snow.order.entity.Food;
import com.snow.order.log.Log;

public class information extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public information() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=GBK");
		int pageNumber=1;
		int pageSize=4;
		int pageAll=0;
		int allPage=0;
		
		
		
		//PrintWriter out = response.getWriter();
		//request.setCharacterEncoding("utf-8");
		try{
			
			List<Food> foodlist=new ArrayList<Food>();		
			FoodBiz foodbiz=new FoodBiz();
			if(request.getParameter("p")==null){
				
				pageNumber=1;
				allPage=foodbiz.getNumber();
				if(allPage%pageSize==0){
					pageAll=allPage/pageSize;
				}
				else{
					pageAll=allPage/pageSize+1;
				}
				
			}
			else{
				
				pageNumber=Integer.parseInt(request.getParameter("p"));
				
				pageAll=Integer.parseInt(request.getParameter("ap"));
				allPage=Integer.parseInt(request.getParameter("apg"));
				if(pageNumber>pageAll){
					pageNumber=pageNumber-1;
				}
				if(pageNumber==0){
					pageNumber=1;
				}
				//System.out.println("pageNumber="+pageNumber);
				
			}
			
			CachedRowSet rs = foodbiz.getFood(pageSize, pageNumber);
			
			try {
				while (rs.next()) {
					 int no=rs.getInt(1);
					  //String proname=rs.getString(2);
					  String foodname=rs.getString(2);
					  Float foodprice=rs.getFloat(3);
					  Float foodcount=rs.getFloat(4);
					  
					  
						foodlist.add(new Food(no,foodname,foodprice,foodcount));
					
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				
					
					String pageit="pageit";
					request.setAttribute("pageNumber", pageNumber);
					request.setAttribute("allPage", allPage);
					request.setAttribute("pageAll", pageAll);
					request.setAttribute("pageit", pageit);
					//鏉烆剙褰�
					
					/*for(int i=0;i<foodlist.size();i++){
						System.out.println("foodlist"+foodlist.get(i).getFodNo());
					}*/

					
					request.setAttribute("foodlist", foodlist);
					request.getRequestDispatcher("cy/Manage.jsp").forward(request, response);
				
			} catch (Exception e) {
				Log.logger.error(e.getMessage());
				e.printStackTrace();
				request.setAttribute("errMsg","");
				request.getRequestDispatcher("/error.jsp").forward(request, response);
			}			
		
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
